// Appendix8.4
log using Appendix8.4.log, replace
use "C:\Users\sbstjp\OneDrive - Cardiff University\voter_panel.dta" // Democracy Fund Voter Study Group. VIEWS OF THE ELECTORATE RESEARCH SURVEY. Washington, D.C. https://www.voterstudygroup.org/.  Date accessed: March 09, 2025.

// Create liberal democracy scale
*Rename so variable names don't get too long
rename govpower_censormedia_2019Nov censormedia
rename govpower_criticizepres_2019Nov critpres
rename govpower_suspendcongress_2019Nov suspendcongress
rename govpower_suspendlaws_2019Nov suspendlaws
rename govpower_suspendrights_2019Nov suspendrights

* Delete "don't know" responses 
foreach var in censormedia critpres suspendcongress suspendlaws suspendrights {
    replace `var' = . if `var' >4
}

*Reverse variable so liberal values coded high
foreach var in censormedia critpres {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in rcensormedia rcritpres suspendcongress suspendlaws suspendrights {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values and means a 0-1 standardization scale isn't feasible as missing values would overlap with the scale
foreach var in srcensormedia srcritpres ssuspendcongress ssuspendlaws ssuspendrights {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreLD = 0
gen count_nonzeroLD = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in srcensormedia srcritpres ssuspendcongress ssuspendlaws ssuspendrights {
    replace total_scoreLD = total_scoreLD + `var'
    replace count_nonzeroLD = count_nonzeroLD + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen LibDemScale = .
replace LibDemScale = total_scoreLD / count_nonzeroLD if count_nonzeroLD > 0

// Create social justice scale
*Delete missing
replace ft_blm_2020Sep=. if ft_blm_2020Sep>100
replace reparations_2020Sep=. if reparations_2020Sep>2
replace defundpolice_2020Sep=. if defundpolice_2020Sep>2 
replace police_threat_2020Sep=. if police_threat_2020Sep>2
replace usa_founders_2020Sep=. if usa_founders_2020Sep>2
replace internetharass_dem_2020Sep=. if internetharass_dem_2020Sep==9

*Reverse coding so social justice values are coded high
foreach var in reparations_2020Sep defundpolice_2020Sep usa_founders_2020Sep {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' - `var'
}

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in ft_blm_2020Sep rreparations_2020Sep rdefundpolice_2020Sep rusa_founders_2020Sep police_threat_2020Sep internetharass_dem_2020Sep {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

*At this stage, the scale has a Cronbach's alpha of 0.83

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values
foreach var in sft_blm_2020Sep srreparations_2020Sep srdefundpolice_2020Sep srusa_founders_2020Sep spolice_threat_2020Sep sinternetharass_dem_2020Sep {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreSJV = 0
gen count_nonzeroSJV = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in sft_blm_2020Sep srreparations_2020Sep srdefundpolice_2020Sep srusa_founders_2020Sep spolice_threat_2020Sep sinternetharass_dem_2020Sep {
    replace total_scoreSJV = total_scoreSJV + `var'
    replace count_nonzeroSJV = count_nonzeroSJV + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen socJusValues = .
replace socJusValues = total_scoreSJV / count_nonzeroSJV if count_nonzeroSJV > 0

// Liberalism scale
*Delete missing values
replace immi_contribution_2020Nov=. if immi_contribution_2020Nov==4 
replace immi_muslim_2020Nov=. if immi_muslim_2020Nov==5 
replace view_abortlegal_2020Nov=. if view_abortlegal_2020Nov==4 
replace view_gaymar_2020Nov=. if view_gaymar_2020Nov==3 


*Reverse coding so liberal values are coded high
foreach var in immi_contribution_2020Nov view_abortlegal_2020Nov view_gaymar_2020Nov rm1_enriched_2020Nov {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in rrm1_enriched_2020Nov rview_gaymar_2020Nov rview_abortlegal_2020Nov rimmi_contribution_2020Nov sexism_complain_2020Nov sexism_equality_2020Nov immi_muslim_2020Nov {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}


* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values
foreach var in srrm1_enriched_2020Nov srview_gaymar_2020Nov srview_abortlegal_2020Nov srimmi_contribution_2020Nov ssexism_complain_2020Nov ssexism_equality_2020Nov simmi_muslim_2020Nov {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreAL = 0
gen count_nonzeroAL = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in srrm1_enriched_2020Nov srview_gaymar_2020Nov srview_abortlegal_2020Nov srimmi_contribution_2020Nov ssexism_complain_2020Nov ssexism_equality_2020Nov simmi_muslim_2020Nov {
    replace total_scoreAL = total_scoreAL + `var'
    replace count_nonzeroAL = count_nonzeroAL + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen libValues = .
replace libValues = total_scoreAL / count_nonzeroAL if count_nonzeroAL > 0

// Demographics
*Delete missing values, generate age variable and rename
replace faminc_2020Sep=. if faminc_2020Sep>20 
gen age = 2020 - birthyr_2020Sep 
rename gender_2020Sep FemaleGender 
replace ideo5_2020Sep=. if ideo5_2020Sep==6 

*Generate dummy variables
gen Graduate=.
replace Graduate=1 if inlist(educ_2020Sep, 5, 6) 
replace Graduate=0 if inrange(educ_2020Sep, 1, 4)

gen BIPOC=.
replace BIPOC=0 if race_2020Sep==1
replace BIPOC = 1 if inrange(race_2020Sep, 2, 8) 

// Standardize variables
egen Age = std(age)
egen Income = std(faminc_2020Sep)
egen SocJusValues = std(socJusValues)
egen LibValues = std(libValues)

// Regressions
regress LibDemScale SocJusValues [pweight=weight_allpanel_2020Nov], robust
eststo
regress LibDemScale Age BIPOC FemaleGender Graduate Income SocJusValues [pweight=weight_allpanel_2020Nov], robust 
eststo
regress LibDemScale LibValues [pweight=weight_allpanel_2020Nov], robust
eststo
regress LibDemScale Age BIPOC FemaleGender Graduate Income LibValues [pweight=weight_allpanel_2020Nov], robust 
eststo
regress LibDemScale Age BIPOC FemaleGender Graduate Income LibValues SocJusValues [pweight=weight_allpanel_2020Nov], robust 
eststo
esttab

log close

eststo clear

esttab using "Appendix8.4.rtf", ///
    b(3) se(3) ///
    star(* 0.05 ** 0.01 *** 0.001) ///
    coeflabels(_cons "Constant") ///
    nonumbers replace

// Alternative specification
ologit LibDemScale Age BIPOC FemaleGender Graduate Income SocJusValues [pweight=weight_allpanel_2020Nov], robust 

